<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <!--页头-->
    <div class="content-header">
        <h1>知识库<small>膳食库</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>知识库</el-breadcrumb-item>
            <el-breadcrumb-item>膳食库</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <!--页面内容-->
    <div class="app-container">
        <div class="box">
            <!--分页头-->
            <div class="filter-container">
                <el-input placeholder="编码/名称/助记码" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage(1)" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <!--分页数据-->
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="code" label="编码" align="center"></el-table-column>
                <el-table-column prop="name" label="食品名称" align="center"></el-table-column>





                <el-table-column prop="type" label="所属类型" align="center"></el-table-column>
                <el-table-column prop="energy" label="100g含能量" align="center"></el-table-column>
                <el-table-column prop="protein" label="蛋白质含量" align="center"></el-table-column>
                <el-table-column prop="fat" label="脂肪含量" align="center"></el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页栏-->
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!--新增弹出种类-->

            <div class="add-form">
                <el-dialog title="新增种类" :visible.sync="dialogFormVisible6" v-if="dialogFormVisible6">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm4" :model="formData4" :rules="rules" label-position="right" label-width="100px">
                                    <el-row>

                                        <el-col :span="12">
                                            <el-form-item label="种类名称" prop="type">
                                                <el-input v-model="formData4.type"/>
                                            </el-form-item>
                                        </el-col>


                                    </el-row>


                                </el-form>
                            </el-tab-pane>

                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible6 = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd6()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增膳食库" :visible.sync="dialogFormVisible" v-if="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                    <el-row>

                                        <el-col :span="12">
                                            <el-form-item label="食品名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="编码" prop="code">
                                                <el-input v-model="formData.code"/>
                                            </el-form-item>
                                        </el-col>



                                            <el-col :span="12" >
                                                <el-form-item label="所属种类">
                                                    <el-select v-model="formData.type" placeholder="请选择">
                                                        <el-option
                                                                v-for="item in options_type"
                                                                :key="item.name"
                                                                :label="item.name"
                                                                :value="item.name">
                                                        </el-option>


                                                    </el-select>
                                                    <el-button type="primary" icon="el-icon-edit" @click="departmentsAdd()" circle>添加种类</el-button>
                                                </el-form-item>
                                            </el-col>






                                        <el-col :span="12">
                                        <el-form-item label="每Kcal能量" prop="energy">
                                            <el-input v-model="formData.energy"/>


                                        </el-form-item>
                                    </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="每KJ能力" prop="capacity">
                                                <el-input v-model="formData.capacity"/>


                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="碳水化合含量" prop="carbohydrate">
                                                <el-input v-model="formData.carbohydrate"/>


                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="脂肪含量" prop="fat">
                                                <el-input v-model="formData.fat"/>


                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="蛋白质含量" prop="protein">
                                                <el-input v-model="formData.protein"/>


                                            </el-form-item>
                                        </el-col>




                                        <template v-for="illness in formData.illnessList">
                                            <el-col :span="12" >

                                                <el-form-item label="">

                                                    <span slot="label">{{illness.name}}</span>


                                                    <el-select v-model="illness.value">
                                                        <el-option label="是" value="1"></el-option>
                                                        <el-option label="否" value="0"></el-option>

                                                    </el-select>

                                                </el-form-item>
                                            </el-col>
                                           </template>






                                        <el-col >
                                            <el-form-item label="说明" prop="state">


                                                <el-input
                                                        type="textarea"
                                                        :rows="2"
                                                        placeholder="请输入内容"
                                                        v-model="formData.state">
                                                </el-input>

                                            </el-form-item>
                                        </el-col>


                                    </el-row>


                                </el-form>
                            </el-tab-pane>

                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增膳食库" :visible.sync="dialogFormVisible4Edit" v-if="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                    <el-row>

                                        <el-col :span="12">
                                            <el-form-item label="食品名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="编码" prop="code">
                                                <el-input v-model="formData.code"/>
                                            </el-form-item>
                                        </el-col>



                                        <el-col :span="12" >
                                            <el-form-item label="所属种类">
                                                <el-select v-model="formData.type" placeholder="请选择">
                                                    <el-option
                                                            v-for="item in options_type"
                                                            :key="item.name"
                                                            :label="item.name"
                                                            :value="item.name">
                                                    </el-option>


                                                </el-select>

                                            </el-form-item>
                                        </el-col>






                                        <el-col :span="12">
                                            <el-form-item label="每Kcal能量" prop="energy">
                                                <el-input v-model="formData.energy"/>


                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="每KJ能力" prop="capacity">
                                                <el-input v-model="formData.capacity"/>


                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="碳水化合含量" prop="carbohydrate">
                                                <el-input v-model="formData.carbohydrate"/>


                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="脂肪含量" prop="fat">
                                                <el-input v-model="formData.fat"/>


                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="蛋白质含量" prop="protein">
                                                <el-input v-model="formData.protein"/>


                                            </el-form-item>
                                        </el-col>




                                        <template v-for="illness in formData.illnessList">
                                            <el-col :span="12" >

                                                <el-form-item label="">

                                                    <span slot="label">{{illness.name}}</span>


                                                    <el-select v-model="illness.value">
                                                        <el-option label="是" value="1"></el-option>
                                                        <el-option label="否" value="0"></el-option>

                                                    </el-select>

                                                </el-form-item>
                                            </el-col>
                                        </template>







                                        <el-col >
                                            <el-form-item label="说明" prop="state">


                                                <el-input
                                                        type="textarea"
                                                        :rows="2"
                                                        placeholder="请输入内容"
                                                        v-model="formData.state">
                                                </el-input>

                                            </el-form-item>
                                        </el-col>


                                    </el-row>


                                </el-form>
                            </el-tab-pane>

                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit= false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data:{
            activeName:'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize:10,
                total:100,
                queryString:null,
            },


            options_type:[],
            options_warning: [{
        value: '一级',
        label: '一级'
    }, {
        value: '二级',
        label: '二级'
    }, {
        value: '三级',
        label: '三级'
    }, {
        value: '四级',
        label: '四级'
    }, {
        value: '五级',
        label: '五级'
    }],



            options_time: [{
                value: '立即',
                label: '立即'
            }, {
                value: '一周内',
                label: '一周内'
            }, {
                value: '一个月内',
                label: '一个月内'
            }, {
                value: '三个月内',
                label: '三个月内'
            }, {
                value: '半年内',
                label: '半年内'
            },
                {
                    value: '一年内',
                    label: '一年内'
                }],


            formData1:{



            },//疾病种类



            options_type:[],//所属种类列表

            dataList: [],//列表数据

            formData4: {},//表单数据
            formData: {},//表单数据
            tableData:[],//新增和编辑表单中对应的检查项列表数据
            checkitemIds:[],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit:false,//控制编辑窗口显示/隐藏
            dialogFormVisible6:false,//添加科室的窗口显示
            rules: {//校验规则
                code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
                name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }],
                sex: [{ required: true, message: '使用性别为必填项', trigger: 'change' }],
                helpCode: [{ required: true, message: '助记码为必填项', trigger: 'blur' }],
                remark: [{ required: true, message: '项目说明为必填项', trigger: 'blur' }],
                attention: [{ required: true, message: '注意事项为必填项', trigger: 'blur' }],
            }
        },

        created() {


            axios.get("/food/findAllIllness.do").then((res)=>{

                this.formData = res.data.data;

            axios.get("/food/findAllType.do").then((res)=>{
                //回显所属种类
                this.options_type = res.data.data;




        });
        });


            this.findPage();
        },


        methods: {
            //编辑
            handleEdit() {




                axios.post("/food/edit.do?id="+this.formData.id,this.formData).then((res)=>{
                    if (res.data.flag){
                    //弹出提示信息
                    this.$message({
                        message:res.data.message,
                        type:'success'
                    });
                }else {
                    //失败
                    this.$message.error(res.data.message);
                }
            }).finally(()=>{
                    //新增成功或失败后，重新调用分页查询方法，查询新的数据
                    this.findPage();
                //关闭编辑窗口
                this.dialogFormVisible4Edit = false;
            });
            },
            //添加
            handleAdd () {
                //全局校验  添加疾病库
                this.$refs['dataAddForm'].validate((valid)=> {
                    if (valid) {
                        //发送ajax请求，将数据传到后台



                        axios.post("/food/add.do", this.formData).then((res) => {
                            if (res.data.flag) {
                            //弹出提示信息
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                        } else {
                            //失败
                            this.$message.error(res.data.message);
                        }
                    }).finally(() => {
                            //成功,关闭新增窗口
                            this.dialogFormVisible = false;
                        //新增成功后，重新调用分页查询方法，查询新的数据
                        this.findPage();
                    });
                    }else {
                        //校验不通过，return，false
                        this.$message.error("数据校验失败，请检查");
                return false;
            }
            });
            },
            handleAdd6 () {

                //全局校验  添加食物种类库
                this.$refs['dataAddForm4'].validate((valid)=> {
                    if (valid) {
                        //发送ajax请求，将数据传到后台
                        axios.post("/food/addDepartments.do?", this.formData4).then((res) => {
                            if (res.data.flag) {
                            //弹出提示信息
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                        } else {
                            //失败
                            this.$message.error(res.data.message);
                        }
                    }).finally(() => {
                            //成功,关闭新增窗口
                            this.dialogFormVisible6 = false;
                        //新增成功后，重新调用分页查询方法，查询新的数据
                        this.dialogFormVisible = false;
                        this.findPage();
                    });
                    }else {
                        //校验不通过，return，false
                        this.$message.error("数据校验失败，请检查");
                return false;
            }
            });
            },
            //分页查询
            findPage(page) {
                if (page == 1){
                    this.pagination.currentPage = page;
                }
                var param= {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/food/findPage.do",param).then((res)=>{
                    this.pagination.total = res.data.total;
                this.dataList = res.data.rows;
            });

            },
            //添加科室
            departmentsAdd(){
                this.dialogFormVisible6 = true;


            },
            // 重置表单
            resetForm() {
                this.formData = {};
                //重置为基本信息
                this.activeName = 'first';
                //重置复选框
                this.checkitemIds = [];
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
                //回显疾病库
                axios.get("/food/findAllIllness.do").then((res)=>{

                    this.formData = res.data.data;

                axios.get("/food/findAllType.do").then((res)=>{
                    //回显所属种类
                    this.options_type = res.data.data;




            });
            });




            },
            // 弹出编辑窗口
            handleUpdate(row) {


                this.dialogFormVisible4Edit = true;
                //重置为选择基本信息
                this.activeName = 'first';
                //发送ajxs请求查询
                axios.get("/food/findById.do?id="+row.id).then((res)=>{
                    if (res.data.flag){
                    //进行回显
                    this.formData = res.data.data;
                }else {
                    //查询失败
                    this.$message.error(res.data.message);
                }
            });


            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            showMessage(res){
                if(res == 'Error: Request failed with status code 403'){
//权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            // 删除
            handleDelete(row) {


                this.$confirm("你确定要删除当前数据吗？",'提示',{
                    type: 'warning'
                }).then(() => {
                    //调用时发送异步求
                    axios.get("/food/deleteById.do?id="+row.id).then((res)=>{
                    if (res.data.flag){
                    //新增成功后，重新调用分页查询方法，查询新的数据
                    this.findPage();
                    //弹出提示信息
                    this.$message({
                        message:res.data.message,
                        type:'success'
                    });
                }else {
                    //失败
                    this.$message.error(res.data.message);
                }
            }).catch((res)=>{
                    this.showMessage(res);
            });
            }).catch(() => {
                    this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });



            }
        }
    })
</script>




</html>
</html>